<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\Plugin\AdminUser\Action\Admin\Index;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\App\Tenant\Contract\TenantContract;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Request\ValidateSceneTrait;
use MoChat\Plugin\AdminUser\Contract\AdminUserContract;
use MoChat\Plugin\AdminUser\Middleware\AdminAuthMiddleware;
use MoChat\Plugin\AdminUser\Middleware\PermissionMiddleware;

/**
 * 超级管理员-系统首页数据折线统计图.
 *
 * Class SysIndex.
 * @Controller
 */
class LineChat extends AbstractAction
{
    use ValidateSceneTrait;

    /**
     * @Inject
     * @var TenantContract
     */
    protected $tenantService;

    /**
     * @Inject
     * @var AdminUserContract
     */
    protected $adminUserService;

    /**
     * @RequestMapping(path="/admin/adminUser/lineChat", methods="get")
     * @Middlewares({
     *     @Middleware(AdminAuthMiddleware::class),
     *     @Middleware(PermissionMiddleware::class)
     * })
     * @return array 返回数组
     */
    public function handle(): array
    {
        // 参数验证
        $this->validated($this->request->all());
        $adminUser = user();
        $firstDay = date('Y-m-d', strtotime(date('Y-m', time()) . '-01 00:00:00'));
        $endDay = date('Y-m-d', strtotime(date('Y-m', time()) . '-' . date('t', time()) . ' 00:00:00'));
        $days = [];
        for ($i = 0; $i <= 31; ++$i) {
            $date = date('Y-m-d', strtotime($firstDay) + $i * 86400);
            if ($date > $endDay) {
                break;
            }
            $days[] = ['day' => $date];
        }
        // 获取近31天数据
        return $this->getData($days, $adminUser);
    }

    /**
     * 验证规则.
     *
     * @return array 响应数据
     */
    protected function rules(): array
    {
        return [
        ];
    }

    /**
     * 验证错误提示.
     * @return array 响应数据
     */
    protected function messages(): array
    {
        return [
        ];
    }

    /**
     * 获取31天数据.
     */
    private function getData(array $days, array $adminUser): array
    {
        // 查询近31天数据
        foreach ($days as &$item) {
            $secondDay = date('Y-m-d', strtotime($item['day']) + 86400);
            // 所有租户
            $item['totalTenant'] = $this->tenantService->countTenantByOwnerId([$adminUser['id']], $item['day'], $secondDay);
            // 代理商
            $item['agent'] = $this->adminUserService->countAdminUserByParentId($adminUser['id'], 1, $item['day'], $secondDay);
            // 付费租户
            $item['payTenant'] = $this->tenantService->countTenantByOwnerId([$adminUser['id']], $item['day'], $secondDay, 1);
            // 未付费租户
            $item['nopayTenant'] = $this->tenantService->countTenantByOwnerId([$adminUser['id']], $item['day'], $secondDay, 2);
        }

        return $days;
    }
}
